import * as Tdt from "./maplaoder/Tianditu_Loader";
import * as GD from "./maplaoder/Gaode_Loader";
import BaiduImageryProvider from "./maplaoder/BaiduImageryProvider";

import { useCesium } from "@gvol-org/daassdk.cesium";

//天地图影像+注记+国界
const load_tdt_imagery = (viewer) => {
  viewer.imageryLayers.addImageryProvider(Tdt.tdt_load_yx());
  viewer.imageryLayers.addImageryProvider(Tdt.tdt_load_yx_zj());
  viewer.imageryLayers.addImageryProvider(Tdt.tdt_contrayBorder());
};

//天地图矢量+注记+国界
const load_tdt_vector = (viewer) => {
  viewer.imageryLayers.addImageryProvider(Tdt.tdt_load_sl());
  viewer.imageryLayers.addImageryProvider(Tdt.tdt_load_sl_zj());
  viewer.imageryLayers.addImageryProvider(Tdt.tdt_contrayBorder());
};

const load_gaode_img = (viewer) => {
  viewer.imageryLayers.addImageryProvider(
    GD.gd_load_layer({
      type: "img_w",
      crs: "WGS84",
    })
  );

  viewer.imageryLayers.addImageryProvider(
    GD.gd_load_layer({
      type: "road_label_w",
      crs: "WGS84",
    })
  );
};

const load_gaode_vector = (viewer) => {
  viewer.imageryLayers.addImageryProvider(
    GD.gd_load_layer({
      type: "vec_label_w",
      crs: "WGS84",
    })
  );
};

const load_baidu_imagery = (viewer) => {
  viewer.imageryLayers.addImageryProvider(
    new BaiduImageryProvider({
      type: "img_w",
      crs: "WGS84",
    })
  );

  viewer.imageryLayers.addImageryProvider(
    new BaiduImageryProvider({
      type: "img_label_w",
      crs: "WGS84",
    })
  );
};

const load_baidu_vector = (viewer) => {
  viewer.imageryLayers.addImageryProvider(
    new BaiduImageryProvider({
      type: "vec_w",
      crs: "WGS84",
    })
  );

  viewer.imageryLayers.addImageryProvider(
    new BaiduImageryProvider({
      type: "vec_label_w",
      crs: "WGS84",
    })
  );
};

const load_esri_img = (viewer) => {
  Cesium.ArcGisMapServerImageryProvider.fromUrl("https://services.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer").then((provider) => {
    viewer.imageryLayers.addImageryProvider(provider);
  });
};
const load_esri_vec = (viewer) => {
  Cesium.ArcGisMapServerImageryProvider.fromUrl("https://services.arcgisonline.com/arcgis/rest/services/World_Street_Map/MapServer").then((provider) => {
    viewer.imageryLayers.addImageryProvider(provider);
  });
};

const load_esri_terr = (viewer) => {
  Cesium.ArcGisMapServerImageryProvider.fromUrl("https://services.arcgisonline.com/arcgis/rest/services/World_Physical_Map/MapServer").then((provider) => {
    viewer.imageryLayers.addImageryProvider(provider);
  });
};

// ------------------------------------   腾讯地图   -----------------------------------------------
// 矢量图
// url: "https://rt3.map.gtimg.com/tile?z={z}&x={x}&y={reverseY}&styleid=1&version=297",
// 黑色风格
// url: "https://rt3.map.gtimg.com/tile?z={z}&x={x}&y={reverseY}&styleid=4&scene=0",
// 注记图1
// url: "https://rt3.map.gtimg.com/tile?z={z}&x={x}&y={reverseY}&styleid=3&scene=0",
// 注记图2
// url: "https://rt3.map.gtimg.com/tile?z={z}&x={x}&y={reverseY}&styleid=2&version=297",

const load_tx_img = (viewer) => {
  viewer.imageryLayers.addImageryProvider(
    new Cesium.UrlTemplateImageryProvider({
      url: "https://p2.map.gtimg.com/sateTiles/{z}/{sx}/{sy}/{x}_{reverseY}.jpg?version=400",
      customTags: {
        sx: function (imageryProvider, x, y, level) {
          return x >> 4;
        },
        sy: function (imageryProvider, x, y, level) {
          return ((1 << level) - y) >> 4;
        },
      },
    })
  );

  viewer.imageryLayers.addImageryProvider(
    new Cesium.UrlTemplateImageryProvider({
      url: "https://rt3.map.gtimg.com/tile?z={z}&x={x}&y={reverseY}&styleid=3&scene=0",
      customTags: {
        sx: function (imageryProvider, x, y, level) {
          return x >> 4;
        },
        sy: function (imageryProvider, x, y, level) {
          return ((1 << level) - y) >> 4;
        },
      },
    })
  );
};

const load_tx_vec = (viewer) => {
  viewer.imageryLayers.addImageryProvider(
    new Cesium.UrlTemplateImageryProvider({
      url: "https://rt3.map.gtimg.com/tile?z={z}&x={x}&y={reverseY}&styleid=1&version=297",
      customTags: {
        sx: function (imageryProvider, x, y, level) {
          return x >> 4;
        },
        sy: function (imageryProvider, x, y, level) {
          return ((1 << level) - y) >> 4;
        },
      },
    })
  );

  viewer.imageryLayers.addImageryProvider(
    new Cesium.UrlTemplateImageryProvider({
      url: "https://rt3.map.gtimg.com/tile?z={z}&x={x}&y={reverseY}&styleid=2&version=297",
      customTags: {
        sx: function (imageryProvider, x, y, level) {
          return x >> 4;
        },
        sy: function (imageryProvider, x, y, level) {
          return ((1 << level) - y) >> 4;
        },
      },
    })
  );
};

//中科星图影像
const load_xingtu_img = (viewer) => {
  viewer.imageryLayers.addImageryProvider(
    new Cesium.UrlTemplateImageryProvider({
      url: "https://tiles{s}.geovisearth.com/base/v1/img/{z}/{x}/{y}?format=webp&token=d4f66dc1acaf3fae9a7c482a92ed15e90a5fad8be591c8369f5361ac8ba67096",
      subdomains: "123",
    })
  );

  viewer.imageryLayers.addImageryProvider(
    new Cesium.UrlTemplateImageryProvider({
      url: "https://tiles{s}.geovisearth.com/base/v1/cia/{z}/{x}/{y}?format=webp&token=d4f66dc1acaf3fae9a7c482a92ed15e90a5fad8be591c8369f5361ac8ba67096",
      subdomains: "123",
    })
  );
};

//中科星图影像
const load_xingtu_vec = (viewer) => {
  viewer.imageryLayers.addImageryProvider(
    new Cesium.UrlTemplateImageryProvider({
      url: "https://tiles{s}.geovisearth.com/base/v1/vec/{z}/{x}/{y}?format=webp&token=d4f66dc1acaf3fae9a7c482a92ed15e90a5fad8be591c8369f5361ac8ba67096",
      subdomains: "123",
    })
  );
};

//中科星图影像
const load_xingtu_ter = (viewer) => {
  viewer.imageryLayers.addImageryProvider(
    new Cesium.UrlTemplateImageryProvider({
      url: "https://tiles{s}.geovisearth.com/base/v1/ter/{z}/{x}/{y}?format=webp&token=d4f66dc1acaf3fae9a7c482a92ed15e90a5fad8be591c8369f5361ac8ba67096",
      subdomains: "123",
    })
  );
};

export const loadBaseMap = (viewer, type) => {
  viewer.imageryLayers.removeAll(true);

  switch (type) {
    case "tdt_img":
      load_tdt_imagery(viewer);
      break;
    case "tdt_vec":
      load_tdt_vector(viewer);
      break;
    case "gaode_img":
      load_gaode_img(viewer);
      break;
    case "gaode_vec":
      load_gaode_vector(viewer);
      break;
    case "baidu_img":
      load_baidu_imagery(viewer);
      break;
    case "baidu_vec":
      load_baidu_vector(viewer);
      break;
    case "esri_img":
      load_esri_img(viewer);
      break;
    case "esri_vec":
      load_esri_vec(viewer);
      break;
    case "esri_phys":
      load_esri_terr(viewer);
      break;
    case "tx_img":
      load_tx_img(viewer);
      break;
    case "tx_vec":
      load_tx_vec(viewer);
      break;
    case "xt_img":
      load_xingtu_img(viewer);
      break;
    case "xt_vec":
      load_xingtu_vec(viewer);
      break;
    case "xt_ter":
      load_xingtu_ter(viewer);
      break;
    default:
      load_tdt_imagery(viewer);
      break;
  }
};
